var path = require('path');
var webpack = require('webpack');
var config = require('./config');
var autoprefixer = require('autoprefixer');
var utils = require('./scripts/utils.js');
var isProduction = process.env.NODE_ENV === 'production';
var cssLoaders = utils.cssLoaders({ extract: isProduction });

if (isProduction) {
  config = config.prod;
} else {
  config = config.dev;
}

module.exports = {
  entry: {
    app: [path.resolve(__dirname, 'src/common/js/polyfills'), path.resolve(__dirname, 'src/pages/app/main.js')],
    'share-bigo': path.resolve(__dirname, 'src/pages/share/bigo.js'),
    'share-tks': path.resolve(__dirname, 'src/pages/share/tks.js'),
  },
  resolve: {
    root: [
      path.join(__dirname, 'src'),
      path.join(__dirname, 'node_modules')
    ],
    extensions: ['', '.js', '.vue']
  },
  resolveLoader: {
    root: path.join(__dirname, 'node_modules')
  },
  output: {
    path: config.assetsRoot,
    filename: utils.assetsPath('js/[name].js'),
    publicPath: config.assetsPublicPath
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        exclude: /node_modules/
      },
      {
        test: /\.vue$/,
        loader: 'vue'
      },
      {
        test: /\.css$/,
        loader: cssLoaders.css
      },
      {
        test: /\.scss$/,
        loader: cssLoaders.scss
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('images/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  postcss: [
    autoprefixer({ browsers: ['last 3 versions'] })
  ],
  vue: {
    autoprefixer: {
      browsers: ['last 3 versions']
    },
    loaders: cssLoaders
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env':{
        'NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
      }
    }),
    utils.injectHtml({
      title: '看SNH48风尚大赏，就上微鲸VR',
      chunks: ['app'],
      filename: 'index.html',
      template: 'src/index.html'
    }, isProduction),
    utils.injectHtml({
      title: '看SNH48风尚大赏，就上微鲸VR',
      chunks: ['share-bigo'],
      filename: 'share-bigo.html',
      template: 'src/index.html'
    }, isProduction),
    utils.injectHtml({
      title: '看SNH48风尚大赏，就上微鲸VR',
      chunks: ['share-tks'],
      filename: 'share-tks.html',
      template: 'src/index.html'
    }, isProduction),    
  ]
};
